package com.example.sonny.dutest;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.sonny.Constraint;
import com.example.sonny.dutest.interfaces.BaseActivityI;
import com.example.sonny.util.AsyncHttpClientUtil;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class SignUpActivity extends BaseActivity implements BaseActivityI{

    private String name;
    private String email;
    private String password;
    private String reppass;

    private Button btn_signup;
    private EditText et_name;
    private  EditText et_pass;
    private EditText et_rep;
    private EditText et_email;

    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up_screen);

        initView();
        initListener();

    }


    @Override
    public void preInit() {

    }

    public void initView() {
        btn_signup = (Button)findViewById(R.id.btnSignUp_screen);
        et_name = (EditText)findViewById(R.id.etUserName);
        et_email =(EditText)findViewById(R.id.etEmail);
        et_pass =(EditText)findViewById(R.id.etPass);
        et_rep = (EditText)findViewById(R.id.etRep);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

        btn_signup.setOnClickListener(this);

    }

    private void signUpJob() {
        name = et_name.getText().toString();
        email = et_email.getText().toString();
        password = et_pass.getText().toString();
        reppass = et_rep.getText().toString();
        if(name.equalsIgnoreCase("")){
            toast("用户名不能为空");
            return ;
        }
        if(password.equalsIgnoreCase("")){
            toast("密码不能为空");
            return ;
        }
        if(!password.equalsIgnoreCase(reppass)){
            toast("两遍密码输入不正确");
            return;
        }
        //make post api: api/users
        postRegister(name,email,password);
    }


    private void postRegister(String name,String email,String password) {
        AsyncHttpClient client =  AsyncHttpClientUtil.getInstence();
        RequestParams requestParams = new RequestParams();
        requestParams.add("name",name);
        requestParams.add("email",email);
        String pass = Constraint.md5(password);
        requestParams.add("password",pass);
        client.post(Constraint.API_URL + "api/users", requestParams, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                toast("注册成功");
                sp = getSharedPreferences("sign", 0);
                SharedPreferences.Editor editor = sp.edit();
                try {
                    editor.putString("email", response.getString("email"));
                    token = response.getString("email");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                editor.commit();
                startNewActivity(MenuActivity.class);
                finish();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                toast("注册失败");
                return ;
                //TODO
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnSignUp:
                signUpJob();
                break;
        }

    }
}
